package zach.cabot.animated;


import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.ImageView;

public class AnimatedSplashScreen extends Activity {
	
	/**
	 * The thread that is called to process the splash screen events
	 */
	private Thread mSplashThread;
	
	/**Declare the media player for music for the splash screen */
	MediaPlayer logoMusic;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        //Splash screen view
        setContentView(R.layout.splash);
        
        
        final AnimatedSplashScreen sPlashScreen = this;
        
        //Set up image view for the splash screen images to be displayed.
        final ImageView splashImageView = (ImageView) findViewById(R.id.SplashImageView);
        splashImageView.setBackgroundResource(R.drawable.flag);
        
        
        final AnimationDrawable frameAnimation = (AnimationDrawable)splashImageView.getBackground();
        
        //The thread that will be used to wait for the splash
        //screen events to take place
        mSplashThread = new Thread(){
        	
        	@Override
        	public void run(){
        		try{
        			synchronized(this){
        				//Wait the given period of time for the splash
        				//screen to work
        				wait(5000);
        			}
        		}
        		catch(InterruptedException ex){
        		}
        		
        		finish();
        		
        		//Run the next activity
        		Intent intent = new Intent();
        		intent.setClass(sPlashScreen, MainActivity.class);
        		startActivity(intent);
        		stop();
        	}
        };
        
        mSplashThread.start();

        //This will start the GUI Thread for the animation
        splashImageView.post(new Runnable() {
        	
        	@Override
        	public void run() {
        		frameAnimation.start();
        	}
        });
        
        
        /*Start music for splash screen and menu.*/
		logoMusic = MediaPlayer.create(AnimatedSplashScreen.this, R.raw.virgasplashmusic);
		logoMusic.start();
        
    }
    
    
    /**
     * This will process the splash screen touch events
     */
    @Override
    public boolean onTouchEvent(MotionEvent evt){
    	if(evt.getAction() == MotionEvent.ACTION_DOWN){
    		synchronized(mSplashThread){
    			mSplashThread.notifyAll();
    		}
    	}
    	return true;
    }
}